<?php

namespace app\user\controller;
use think\Db;

class Index extends Common
{
    public function ajax_public_select()
    {
        $this->method = array(
            "carousel" => array("Db::tablename" => "carousel", "key" => "id", "page" => '', "order" => '', "where" => array("status" => 1, 'cid' => $this->cid), "field" => 'title,url,image'),
            "service_category" => array("Db::tablename" => "service_category", "key" => "id", "page" => '', "order" => 'sort desc,id desc', "where" => ['gid' => 0], "field" => 'id,title,image'),
            "service_category_sub" => array("Db::tablename" => "service_category", "key" => "id", "page" => '', "order" => '', "where" => ['gid' => input('get.gid')], "field" => 'gid,id,title,image'),
            "service_list_bak" => array("Db::tablename" => "service", "key" => "id", "page" => (input('get.page') ? input('get.page') : 1) . ",10", "order" => 'replace(flag,2,10) desc,time desc', "where" => ['fields' => 'gid,cateid,cid,aid', 'status' => 1,
                function ($query) {
                    $query->where(['flag' => ['<>', 2]])->whereor(
                        function ($query) {
                            $query->where(['flag' => 2, 'flagtime' => ['>=', time()]]);
                        });
                }],
                "field" => 'id,title,image1,price'),
            "service_list_sub" => array("Db::tablename" => "service", "key" => "id", "page" => (input('get.page') ? input('get.page') : 1) . ",10", "order" => 'replace(flag,2,10) desc,time desc', "where" => ['cateid' => input('get.catid'), 'aid' => input('get.aid'), 'status' => 1, function ($query) {
                $query->where(['flag' => ['<>', 2]])->whereor(
                    function ($query) {
                        $query->where(['flag' => 2, 'flagtime' => ['>=', time()]]);
                    });
            }], "field" => 'id,title,image1,price'),
            "area" => array("Db::tablename" => "area", "key" => "id", "page" => '0,10', "order" => 'id desc', "where" => ['status' => 1, 'father_id' => $this->cid], "field" => 'id,name'),
            "service_list" => array("Db::tablename" => "service", "key" => "id", "page" => (input('get.page') ? input('get.page') : 1) . ",10", "order" => 'sort desc,id desc', "where" => ['status' => 1, 'gid' => input('get.catid')], "field" => 'id,title,image1,price,service,volume,stock'),
            "service_hot" => array("Db::tablename" => "service", "key" => "id", "page" => '0,10', "order" => 'id desc', "where" => ['status' => 1, 'flag' => 1, 'flagtime' => ['>=', time()], 'cid' => $this->cid], "field" => 'id,title,image1,service,volume'),
            "service_detail" => array("Db::tablename" => "service", "key" => "id", "page" => '', "order" => '', "where" => ['id' => input('get.id'), 'status' => 1], "field" => ''),
            "seller_detail" => array("Db::tablename" => "user", "key" => "id", "page" => '', "order" => '', "where" => ['id' => input('get.id'), 'type' => 1], "field" => ''),
            "city" => array("Db::tablename" => "city", "key" => "id", "page" => '0,350', "order" => 'capital asc', "where" => ['status' => 1], "field" => 'id,name,capital'),
        );
        if (!array_key_exists($this->m, $this->method)) {
            return json(array('code' => 0, 'msg' => lang('error')));
        }
        if (is_array($this->method[$this->m]['where'])) {
            if (array_key_exists('fields', $this->method[$this->m]['where'])) {
                $getfields = explode(',', $this->method[$this->m]['where']['fields']);
                if (is_array($getfields)) {
                    foreach ($getfields as $field) {
                        if (input('get.' . $field) && input('get.' . $field) != 'null') {
                            $this->method[$this->m]['where'][$field] = input('get.' . $field);
                        }
                    }
                }
                unset($this->method[$this->m]['where']['fields']);
            }
        }
        if ($this->m == "service_detail") {
            $data = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->page($this->method[$this->m]['page'])->order($this->method[$this->m]['order'])->field($this->method[$this->m]['field'])->find();
            $data['spec'] = json_decode($data['spec']);
            $data['attr'] = json_decode($data['attr']);
        } else {
            $data = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->page($this->method[$this->m]['page'])->order($this->method[$this->m]['order'])->field($this->method[$this->m]['field'])->select();
        }
        return json(array('data' => $data));
    }

    public function ajax_private_select()
    {
        $token = input("get.token");
        if (!$token) {
            return json(array('code' => 0, 'msg' => lang('error')));
        } else {
            if (!$uid = Db::table("user")->where(["token" => $token, "status" => 1])->value("id")) {
                return json(array('code' => 0, 'msg' => lang('error')));
            }
        }
        $this->method = array(
            "my_address" => array("Db::tablename" => "address", "func" => "select", "key" => "id", "page" => '', "order" => '', "where" => array("uid" => $uid), "field" => ''),
            "get_address" => array("Db::tablename" => "address", "func" => "find", "key" => "id", "page" => '', "order" => '', "where" => array("uid" => $uid, "id" => input("get.id")), "field" => 'pca,title,address,name,phone,lng,lat'),
            "add_address" => array("Db::tablename" => "address", "func" => "insert", "key" => "id", "page" => '', "order" => '', "where" => '', "field" => '', "data" => ['uid' => $uid, 'fields' => ['pca', 'address', 'name', 'phone']]),
            "edit_address" => array("Db::tablename" => "address", "func" => "update", "key" => "id", "page" => '', "order" => '', "where" => ['id' => input("get.id")], "field" => '', "data" => ['fields' => ['pca', 'address', 'name', 'phone']]),
            "del_address" => array("Db::tablename" => "address", "func" => "delete", "key" => "id", "page" => '', "order" => '', "where" => ['id' => input("get.id")], "field" => '', "data" => []),
            "feeDb::tableack" => array("Db::tablename" => "feeDb::tableack", "func" => "insert", "key" => "id", "page" => '', "order" => '', "where" => '', "field" => '', "data" => ['uid' => $uid, 'time' => time(), 'fields' => ['question', 'score']]),
            "order_detail" => array("Db::tablename" => "order", "func" => "find", "key" => "id", "page" => '', "order" => '', "where" => ['orderid' => input('get.orderid'), "uid" => $uid], "field" => 'id,orderid,price,service_id,service_title,service_image,service_service,service_time,remark,address_address,address_title,time,status,payment', "data" => ''),
            "order_list_status_1" => array("Db::tablename" => "order", "func" => "select", "key" => "id", "page" => (input('get.page') ? input('get.page') : 1) . ",10", "order" => 'paytime desc,time desc', "where" => ['status' => ['<', 4], "uid" => $uid], "field" => 'id,orderid,price,service_id,address_id,service_title,address_title,address_address,time,status', "data" => ''),
            "order_list_status_2" => array("Db::tablename" => "order", "func" => "select", "key" => "id", "page" => (input('get.page') ? input('get.page') : 1) . ",10", "order" => 'time desc', "where" => ['status' => ['>=', 4], "uid" => $uid], "field" => 'id,orderid,price,service_id,address_id,service_title,address_title,address_address,time,status', "data" => ''),
            "order_confrim" => array("Db::tablename" => "order", "func" => "update", "key" => "id", "page" => '', "order" => '', "where" => ['orderid' => input("get.orderid")], "uid" => $uid, 'status' => 3, "field" => '', "data" => ['status' => 4]),
        );
        if (!array_key_exists($this->m, $this->method)) {
            return json(array('code' => 0, 'msg' => lang('error')));
        }
        if (is_array($this->method[$this->m]['where'])) {
            if (array_key_exists('fields', $this->method[$this->m]['where'])) {
                $getfields = explode(',', $this->method[$this->m]['where']['fields']);
                if (is_array($getfields)) {
                    foreach ($getfields as $field) {
                        if (input('get.' . $field) && input('get.' . $field) != 'null') {
                            $this->method[$this->m]['where'][$field] = input('get.' . $field);
                        }
                    }
                }
                unset($this->method[$this->m]['where']['fields']);
            }
        }
        switch ($this->method[$this->m]['func']) {
            case "insert":
                if (is_array($this->method[$this->m]['data'])) {
                    if (array_key_exists('fields', $this->method[$this->m]['data'])) {
                        $getfields = $this->method[$this->m]['data']['fields'];
                        if (is_array($getfields)) {
                            foreach ($getfields as $field) {
                                if (input('get.' . $field) && input('get.' . $field) != 'null') {
                                    $this->method[$this->m]['data'][$field] = input('get.' . $field);
                                } else {
                                    return json(array('code' => 0, 'msg' => lang('error')));
                                }
                            }
                        }
                        unset($this->method[$this->m]['data']['fields']);
                    }
                }
                if ($id = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->insertGetId($this->method[$this->m]['data'])) {
                    return json(array('code' => 1, 'msg' => lang('success')));
                }
                break;
            case "update":
                if (is_array($this->method[$this->m]['data'])) {
                    if (array_key_exists('fields', $this->method[$this->m]['data'])) {
                        $getfields = $this->method[$this->m]['data']['fields'];
                        if (is_array($getfields)) {
                            foreach ($getfields as $field) {
                                if (input('get.' . $field) && input('get.' . $field) != 'null') {
                                    $this->method[$this->m]['data'][$field] = input('get.' . $field);
                                } else {
                                    return json(array('code' => 0, 'msg' => lang('error')));
                                }
                            }
                        }
                        unset($this->method[$this->m]['data']['fields']);
                    }
                }
                if (Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->update($this->method[$this->m]['data'])) {
                    return json(array('code' => 1, 'msg' => lang('success')));
                } else {
                    return json(array('code' => 0, 'msg' => lang('error11')));
                }
                break;
            case "delete":
                $data = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->delete();
                return json(array('code' => 1, 'msg' => lang('success')));
                break;
            case "count":
                $data = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->page($this->method[$this->m]['page'])->order($this->method[$this->m]['order'])->field($this->method[$this->m]['field'])->count();
                break;
            case "select":
                $data = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->page($this->method[$this->m]['page'])->order($this->method[$this->m]['order'])->field($this->method[$this->m]['field'])->select();
                break;
            case "find":
                $data = Db::table($this->method[$this->m]['Db::tablename'])->where($this->method[$this->m]['where'])->page($this->method[$this->m]['page'])->order($this->method[$this->m]['order'])->field($this->method[$this->m]['field'])->find();
                break;
            default:
                return json(array('code' => 0, 'msg' => lang('error')));
        }
        return json(array('data' => $data));
    }
}